package ink.xiaobaibai.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * @description: 统一返回对象
 * @author: 小白白
 * @create: 2021-05-17
 **/

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseFormat<T> {

    private int code;

    private String msg;

    private T data;


    private ResponseFormat(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ResponseFormat(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> ResponseFormat<T> ok(String msg, T data) {
        return new ResponseFormat<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResponseFormat<T> ok(String msg) {
        return new ResponseFormat<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> ResponseFormat<T> success(T data) {
        return new ResponseFormat<>(ResponseCode.SUCCESS.getCode(), "success", data);
    }

    public static <T> ResponseFormat<T> success() {
        return new ResponseFormat<>(ResponseCode.SUCCESS.getCode(), "success");
    }

    public static <T> ResponseFormat<T> fail(String msg) {
        return new ResponseFormat<T>(ResponseCode.ERROR.getCode(), msg);
    }

    public static <T> ResponseFormat<T> failure(String msg) {
        return new ResponseFormat<T>(ResponseCode.ERROR.getCode(), msg);
    }

}
